package cn.edu.csust.coursecard.service.impl;

import cn.edu.csust.coursecard.dao.entity.RecruitInfo;
import cn.edu.csust.coursecard.common.CodeEnum;
import cn.edu.csust.coursecard.common.ReturnData;
import cn.edu.csust.coursecard.dao.RecruitInfoMapper;
import cn.edu.csust.coursecard.exception.BaseException;
import cn.edu.csust.coursecard.service.RecruitInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class RecruitInfoServiceImpl implements RecruitInfoService {
    @Autowired
    RecruitInfoMapper recruitInfoMapper;

    @Override
    public ReturnData addRecruitInfo(RecruitInfo recruitInfo) {
        recruitInfo.setCreateTime(new Date());
        recruitInfo.setUpdateTime(new Date());
        recruitInfo.setIsValid((byte) 1);
        recruitInfoMapper.insertSelective(recruitInfo);
        return ReturnData.success();
    }

    @Override
    public ReturnData deleteRecruitInfo(Integer id) {
        int row = recruitInfoMapper.deleteByPrimaryKey(id);
        if(row!=1){
            throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(),"信息删除失败！");
        }
        return ReturnData.success();
    }

    @Override
    public ReturnData getRecruitInfo(Integer id) {
        return ReturnData.success(recruitInfoMapper.selectByPrimaryKey(id));
    }

    @Override
    public ReturnData getAllRecruitInfo() {
        return ReturnData.success(recruitInfoMapper.selectAll());
    }

    @Override
    public ReturnData getRecruitInfoByTitle(String title) {
        if(StringUtils.isEmpty(title)){
            throw new BaseException(CodeEnum.PARAM_ERROR.getCode(), "参数错误！");
        }
        return ReturnData.success(recruitInfoMapper.selectByTitle(title));
    }
}
